package com.hmkj.core.api.chuanglan.util;

import com.alibaba.fastjson.JSON;
import com.hmkj.core.api.chuanglan.request.SmsSendRequest;
import com.hmkj.core.exception.BussinessException;
import com.xiaoleilu.hutool.util.StrUtil;
import lombok.Setter;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.function.Consumer;

/**
 * 创蓝短信工具类
 *
 * @author Vector
 * @create 2017-09-21 18:48
 */
public class ChuangLanSmsUtil {
    @Setter
    private String signature;   /*平台短信签名*/
    @Setter
    private String username;    /*创蓝短信接口用户名*/
    @Setter
    private String password;    /*创蓝短信接口密码*/
    @Setter
    private String smsUrl;      /*创蓝短信接口地址*/
    @Setter
    private boolean enable;     /*短信接口是否启用*/

    private static ChuangLanSmsUtil instance;

    public ChuangLanSmsUtil(String username, String password, String smsUrl, boolean enable) {
        this.signature = signature;
        this.username = username;
        this.password = password;
        this.smsUrl = smsUrl;
        this.enable = enable;
    }

    public ChuangLanSmsUtil(Consumer<ChuangLanSmsUtil> consumer) {
        consumer.accept(this);
    }

    /**
     * @param phone
     * @param msg
     * @return String
     * @throws
     * @author tianyh
     * @Description
     */
    public String sendSmsByPost(String phone, String msg) {
        if (!enable) return null;
        if (StrUtil.isBlank(phone)) throw new BussinessException("手机号不能为空");
        if (StrUtil.isBlank(msg)) throw new BussinessException("短信内容不能为空");
        SmsSendRequest smsSingleRequest = new SmsSendRequest(username, password, signature + msg, phone, "true");
        URL url = null;
        try {
            url = new URL(smsUrl);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");// 提交模式
            httpURLConnection.setConnectTimeout(10000);//连接超时 单位毫秒
            httpURLConnection.setReadTimeout(10000);//读取超时 单位毫秒
            // 发送POST请求必须设置如下两行
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            httpURLConnection.setRequestProperty("Content-Type", "application/json");

            httpURLConnection.connect();
            OutputStream os = httpURLConnection.getOutputStream();
            os.write(JSON.toJSONString(smsSingleRequest).getBytes("UTF-8"));
            os.flush();

            StringBuilder sb = new StringBuilder();
            int httpRspCode = httpURLConnection.getResponseCode();
            if (httpRspCode == HttpURLConnection.HTTP_OK) {
                // 开始获取数据
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
                String line = null;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                br.close();
                return sb.toString();

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


//    /**
//     * @param phone
//     * @param msg
//     * @return String
//     * @throws
//     * @author tianyh
//     * @Description
//     */
//    public static String sendSmsByPost(String phone, String msg) {
//        String username = "N9258859";
//        String password="fy26ebg9W";
//        String signature ="【惠利在线】";
//        String smsUrl="http://smssh1.253.com/msg/send/json";
//        if (StrUtil.isBlank(phone)) throw new BussinessException("手机号不能为空");
//        if (StrUtil.isBlank(msg)) throw new BussinessException("短信内容不能为空");
//        SmsSendRequest smsSingleRequest = new SmsSendRequest(username, password, signature + msg, phone, "true");
//        URL url = null;
//        try {
//            url = new URL(smsUrl);
//            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
//            httpURLConnection.setRequestMethod("POST");// 提交模式
//            httpURLConnection.setConnectTimeout(10000);//连接超时 单位毫秒
//            httpURLConnection.setReadTimeout(10000);//读取超时 单位毫秒
//            // 发送POST请求必须设置如下两行
//            httpURLConnection.setDoOutput(true);
//            httpURLConnection.setDoInput(true);
//            httpURLConnection.setRequestProperty("Charset", "UTF-8");
//            httpURLConnection.setRequestProperty("Content-Type", "application/json");
//
//            httpURLConnection.connect();
//            OutputStream os = httpURLConnection.getOutputStream();
//            os.write(JSON.toJSONString(smsSingleRequest).getBytes("UTF-8"));
//            os.flush();
//
//            StringBuilder sb = new StringBuilder();
//            int httpRspCode = httpURLConnection.getResponseCode();
//            if (httpRspCode == HttpURLConnection.HTTP_OK) {
//                // 开始获取数据
//                BufferedReader br = new BufferedReader(
//                        new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
//                String line = null;
//                while ((line = br.readLine()) != null) {
//                    sb.append(line);
//                }
//                br.close();
//                return sb.toString();
//
//            }
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//
//    public static void main(String[] args) {
//        System.out.println(ChuangLanSmsUtil.sendSmsByPost("18072850376","您的短信验证码是1234，请注意查收。"));
//        System.out.println(ChuangLanSmsUtil.sendSmsByPost("18072850376","恭喜你能成功投资标的【阿什顿发】，投资金额2000元，请注意查收。"));
//    }
}
